package com.example.springcloud.controller;
//提供Restful服务！！！！

import com.example.springcloud.pojo.User;
import com.example.springcloud.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

public class UserController {

    @Autowired
    private UserService userService;



    @GetMapping("/user/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGet")
    public User get(@PathVariable("id") Long id){
        User user = userService.queryById(id);
        if (user==null){
            throw new RuntimeException("id=>"+id+"不存在该用户，或者信息无法找到");
        }
        return user;
    }
    //备选方案
    public User hystrixGet(@PathVariable("id") Long id){
        return new User().setId(id).setUname("id=>"+id+"没有对应的信息，null--@hystrix")
                .setDb_source("no this database in Mysql");
    }

}
